<?php
/**
* @file $Id: diagnostic.php 457 2007-04-23 05:44:57Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

error_reporting(1);
session_start();
if(!$_SESSION['USERNAME'])
{
	$unset_username = true;
	$_SESSION['USERNAME'] = 'diagnostic';
}
if(!file_exists('./Warehouse.php'))
	$error[] = 'The diagnostic.php file needs to be in the Focus directory to be able to run.  Please move it there, and run it again.';
else
{
	include './Warehouse.php';
	if(!@opendir("$staticpath/functions"))
		$error[] = 'The functions directory does not have the correct permissions to be read by the webserver.';

	if(!function_exists('pg_connect'))
		$error[] = 'Your version of PHP does not include PostgreSQL support.  <UL><LI>If you\'ve install Focus/SIS on a Mac OS X machine, download and install the PHP and PostgreSQL packages from Marc Liyanage\'s <A HREF=http://www.entropy.ch>Entropy.ch</A> site.  <LI>If using Windows, open the php.ini file in a text editor like WordPad and remove the semi-colon before the line ";pg_sql.dll."<LI> If you\'re using a Linux or Unix server, you\'ll probably need to recompile PHP using the --with-pgsql option.</UL>';
	else
	{
		if($DatabaseServer!='localhost')
			$connectstring = "host=$DatabaseServer ";
		$connectstring .= "port=$DatabasePort dbname=$DatabaseName user=$DatabaseUsername";
		if(!empty($DatabasePassword))
			$connectstring.=" password=$DatabasePassword";
		if($connection = pg_connect($connectstring))
		{}
		else
		{
			if($DatabaseServer!='localhost')
				$connectstring = "host=$DatabaseServer port=$DatabasePort ";
			$connectstring .= "dbname=$DatabaseName user=$DatabaseUsername";
			if(!empty($DatabasePassword))
				$connectstring.=" password=$DatabasePassword";
			if($connection = pg_connect($connectstring))
			{}
			else
			{
				$connectstring = "host=$DatabaseServer port=$DatabasePort ";
				$connectstring .= "dbname=$DatabaseName user=$DatabaseUsername";
				if(!empty($DatabasePassword))
					$connectstring.=" password=$DatabasePassword";
				$connection = pg_connect($connectstring);
			}
		}
	
		if(!$connection)
		{
			$error[] = 'Focus/SIS cannot connect to the Postgres database.  Either the database "'.$DatabaseName.'" does not exist, Postgres is not running, it was not started with the -i option, or connections from this host are not allowed in the pg_hba.conf file. <BR><BR> If the database does not exist, create it using phpPGAdmin, PGAdmin III, or by opening a terminal window and following these directions:<OL>
			<LI>Login as the postgres user<BR>
				ex) server$ su - postgres
			<LI>Login to the postgres database server using the template1 database<BR>
				ex) server$ psql template1
			<LI>Create the focus database<BR>
				ex) template1=# CREATE DATABASE focus;';
		}
		else
		{
			$result = @pg_exec($connection,'SELECT * FROM CONFIG');
			if($result===false)
				$errstring = pg_last_error($connection);

			if(strpos($errstring,'config: permission denied')!==false)
				$error[] = 'The database was created with the wrong permissions.  The user specified in the config.inc.php file does not have permission to access the focus database.  Use the super-user (postgres) or recreate the database adding \connect - YOUR_USERNAME to the top of the focus.sql file.';
			elseif(strpos($errstring,'elation "config" does not exist')!==false)
				$error[] = 'At least one of the tables does not exist.  <A HREF=install.php>Click here to install the database.</A>';
			elseif($errstring)
				$error[] = $errstring;
		}
	}
	if(!is_executable($htmldocPath))
		$error[] = _("PDFs cannot be produced since HTMLDOC isn't working.  Please ensure that HTMLDOC is installed and that its path is configured in config.inc.php");
}

echo _ErrorMessage($error,'error');
if(!count($error))
	echo '<h3>Your Focus installation is properly configured.</h3>';
phpinfo();

if($unset_username)
	unset($_SESSION['USERNAME']);

function _ErrorMessage($errors,$code='error')
{
	if($errors) 
	{
		$return .= "<TABLE border=0><TR><TD align=".ALIGN_LEFT.">";
		if(count($errors)==1)
		{
			if($code=='error' || $code=='fatal')
				$return .= '<b><font color=#CC0000>Error:</font></b> ';
			else
				$return .= '<b><font color=#00CC00>Note:</font></b> ';
			$return .= (($errors[0])?$errors[0]:$errors[1]);
		}
		else
		{
			if($code=='error' || $code=='fatal')
				$return .= "<b><font color=#CC0000>Errors:</font></b>";
			else
				$return .= '<b><font color=#00CC00>Note:</font></b>';
			$return .= '<ul>';
			foreach($errors as $value)
				$return .= "<LI><font size=-1>$value</font></LI>\n";
			$return .= '</ul>';
		}
			$return .= "</TD></TR></TABLE><br>";
		
		if($code=='fatal')
		{
			echo $return;
			if(!$_REQUEST['_FOCUS_PDF'])
				Warehouse('footer');
			exit;
		}
		
		return $return;
	}
}

?>